//=========================================================================
// Copyright (C) 2024 The C++ Component Model(COMO) Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//=========================================================================

#include <MemPool.h>

void TestMemPool1()
{
    MemPoolItem items[] = {{10, 64, NULL}, {10, 128, NULL}, {10, 256, NULL}};
    CMemPoolSet *memPoolSet = CMemPoolSet_New(items, 3);
    if (NULL == memPoolSet) {
        //
    }

    void *p;
    for (int i = 0;  i < 100;  i++) {
        p = CMemPoolSet_Alloc(memPoolSet, 28, MUST_USE_MEM_POOL);
        if (i < 20) {
            //EXPECT_NE(p, NULL);
        }

        if ((i % 2) == 0) {
            bool b = memPoolSet->Free(p);
            if (i < 20) {
                //EXPECT_EQ(b, true);
            }
        }
    }

    bool b = CMemPoolSet_Free(memPoolSet, p);
    //EXPECT_EQ(b, false);
}


// TEST(MemPool, TestMemPool2)
// {
//     CMemPoolSet::MemPoolItem items[] = {{10, 64, NULL}, {10, 128, NULL}, {10, 256, NULL}};
//     CMemPoolSet *memPoolSet = new CMemPoolSet(items, 3);
//     EXPECT_NE(memPoolSet, NULL);

//     // std::cout << memPoolSet << std::endl;

//     void *p;
//     for (int i = 0;  i < 100;  i++) {
//         p = memPoolSet->Alloc(28, MUST_USE_MEM_POOL);
//         if(i < 10) EXPECT_NE(NULL, p);
//         else EXPECT_EQ(NULL, p);
//     }

// }

void TestMemPool2()
{
    MemPoolItem items[] = {{10, 64, NULL}, {10, 128, NULL}, {10, 256, NULL}};
    CMemPoolSet *memPoolSet = CMemPoolSet_New(items, 3);
    EXPECT_NE(memPoolSet, NULL);

    // std::cout << memPoolSet << std::endl;

    void *p;
    for (int i = 0;  i < 100;  i++) {
        if (i % 3 == 0)
            p = CMemPoolSet_Alloc(memPoolSet, 64, MUST_USE_MEM_POOL);
        else if (i % 3 == 1)
            p = CMemPoolSet_Alloc(memPoolSet, 128, MUST_USE_MEM_POOL);
        else
            p = CMemPoolSet_Alloc(memPoolSet, 256, MUST_USE_MEM_POOL);

        if (i < 30) {
            //EXPECT_NE(NULL, p);
        }
        else {
            //EXPECT_EQ(NULL, p);
        }
    }

}


void TestMemPool3()
{
    MemPoolItem items[] = {{100, 64, NULL}};
    CMemPoolSet *memPoolSet = CMemPoolSet_New(items, 1);
    //EXPECT_NE(NULL, memPoolSet);
    void *p;
    for (int i = 0; i < 150; i++) {
        p = CMemPoolSet_Alloc(memPoolSet, 64, MUST_USE_MEM_POOL);
        if (i % 2 == 0) {
            bool b = CMemPoolSet_Free(memPoolSet, p);
            //EXPECT_EQ(true, b);
            // b = memPoolSet->Free(p);
            // EXPECT_NE(true, b);
        }
    }

    bool b1 = CMemPool_CheckFull(memPoolSet->m_MemPoolSet[0].memPool);
    //EXPECT_EQ(false, b1);

    for(int i = 0; i < 50; i++) {
        p = CMemPoolSet_Alloc(memPoolSet, 64, MUST_USE_MEM_POOL);
        if (i < 25) {
            //EXPECT_NE(NULL, p);
        }
        else {
            //EXPECT_EQ(NULL, p);
        }
    }
    b1 = CMemPool_CheckFull(memPoolSet->m_MemPoolSet[0].memPool);
    //EXPECT_EQ(true, b1);
}


int main(int argc, char **argv)
{
    TestMemPool1();
    TestMemPool2();
    TestMemPool3();
}
